// pages/personal_mse/address.js

var tcity = require("../../utils/citys.js");

var app = getApp()
Page({
  data: {
    address_detail:"",  //详细地址
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false
  },
  bindChange: function (e) {
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;
    if (val[0] != t[0]) {
      const citys = [];
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name)
      }
      for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        countys.push(cityData[val[0]].sub[0].sub[i].name)
      }

      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0].name,
        citys: citys,
        county: cityData[val[0]].sub[0].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], 0, 0]
      })
      return;
    }
    if (val[1] != t[1]) {
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
      }
      this.setData({
        city: this.data.citys[val[1]],
        county: cityData[val[0]].sub[val[1]].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], val[1], 0]
      })
      return;
    }
    if (val[2] != t[2]) {
      this.setData({
        county: this.data.countys[val[2]],
        values: val
      })
      return;
    }

  },
  open: function () {
    this.setData({
      condition: !this.data.condition
    })
  },
  onLoad: function () {
    var that = this;
    tcity.init(that);
    var cityData = that.data.cityData;
    const provinces = [];
    const citys = [];
    const countys = [];

    for (let i = 0; i < cityData.length; i++) {
      provinces.push(cityData[i].name);
    }
    // console.log('省份完成');
    for (let i = 0; i < cityData[0].sub.length; i++) {
      citys.push(cityData[0].sub[i].name)
    }
    // console.log('city完成');
    for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
      countys.push(cityData[0].sub[0].sub[i].name)
    }

    that.setData({
      'provinces': provinces,
      'citys': citys,
      'countys': countys,
      'province': cityData[0].name,
      'city': cityData[0].sub[0].name,
      'county': cityData[0].sub[0].sub[0].name
    })
    // console.log('初始化完成');
    if (app.globalData.userInfo.address!="")//地址不为空则设置地址
    {
      var d="";
      for (var i=3;i<app.globalData.userInfo.address.split("-").length;i++)
        {
          d = d + app.globalData.userInfo.address.split("-")[i]+"-";
        }
        d=d.substring(0,d.length-1);
      that.setData({
        province: app.globalData.userInfo.address.split("-")[0],
        city: app.globalData.userInfo.address.split("-")[1],
        county: app.globalData.userInfo.address.split("-")[2],
        address_detail:d
      })

    }
   
  },
  adr_change: function (e) { //文本改变是触发
    // console.log(e.detail.value);  //输入的文本框的值,每变动一次执行一次
    this.setData({address_detail: e.detail.value })
  },
  change_address:function(){   //确定改变地址
  var that=this;
  // var adr = that.data.address_detail;
  // console.log(that.data.address_detail);
  if (that.data.address_detail == undefined)
  {
    this.setData({ address_detail:"" })
  }
  if (that.data.province == undefined) {
    this.setData({ province: "北京", city: "北京市", county:"东城区"})
  }
  var adr = that.data.province + "-" + that.data.city + "-"+ that.data.county + "-" + that.data.address_detail
  var s = adr.split("-");
  // console.log(adr);
  // console.log(s[0]);
 
  app.update_person_msg("address", adr);
  app.globalData.userInfo.address = adr;
  wx.navigateBack({//返回上一级
    delta: 1
  })
  }
})
